import 'dart:ui';

import 'package:flukit/flukit.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_first/common/constant.dart';
import 'package:flutter_first/utils/objectUtil.dart';
import 'package:flutter_first/utils/spUtil.dart';
import 'package:flutter_first/utils/timerUtil.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:rxdart/rxdart.dart';

import 'routes.dart';

class Splash extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _SplashState();
}

class _SplashState extends State<Splash> {
  TimerUtil _timerUtil;
  int _status = 0;
  int _seconds = 5;
  List<Widget> _bannerItem = [];

  List<String> _bannerList = [
    'images/guide_img01.jpg',
    'images/guide_img02.jpg',
    'images/guide_img03.jpg',
    'images/guide_img04.jpg'
  ];

  @override
  void initState() {
    super.initState();
    SpUtil.getInstance();
    // 隐藏状态栏和导航栏
    SystemChrome.setEnabledSystemUIOverlays([]);

    // Future延时方法
    // Future.delayed(Duration(milliseconds: 500), () {});

    TimerStream(1, Duration(milliseconds: 2000)).listen((_) {
      if (ObjectUtil.isNotEmpty(_bannerList) &&
          SpUtil.getBool(Constant.key_guide, defValue: true)) {
        SpUtil.putBool(Constant.key_guide, false);
        _initBanner();
      } else {
        _initSplash();
      }
    });
  }

  void _initBanner() {
    _initBannerData();
    setState(() {
      _status = 2;
    });
  }

  void _initBannerData() {
    for (var i = 0; i < _bannerList.length; i++) {
      if (i == _bannerList.length - 1) {
        _bannerItem.add(
          Stack(
            children: [
              Image.asset(
                _bannerList[i],
                fit: BoxFit.fill,
                width: double.infinity,
                height: double.infinity,
              ),
              Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  margin: EdgeInsets.only(bottom: 55.0),
                  child: InkWell(
                    onTap: () => _goMain(),
                    child: Container(
                      width: 150.0,
                      height: 40.0,
                      child: Center(
                        child: Text(
                          '立即体验',
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Colors.white, fontSize: 16.0),
                        ),
                      ),
                      decoration: BoxDecoration(
                          color: Color(0x66000000),
                          borderRadius: BorderRadius.all(Radius.circular(5.0))),
                    ),
                  ),
                ),
              )
            ],
          ),
        );
      } else {
        _bannerItem.add(
          Image.asset(
            _bannerList[i],
            fit: BoxFit.fill,
            width: double.infinity,
            height: double.infinity,
          ),
        );
      }
    }
  }

  void _initSplash() {
    _goMain();
  }

  @override
  Widget build(BuildContext context) {
    // Set the fit size (fill in the screen size of the device in the design)
    // If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
    ScreenUtil.init(context, width: 750, height: 1334);

    return Material(
      child: Stack(
        children: [
          // 控制child是否显示
          // 当offstage为true，控件隐藏； 当offstage为false，显示；
          // 当Offstage不可见的时候，如果child有动画等，需要手动停掉，Offstage并不会停掉动画等操作
          Offstage(
            offstage: _status != 0,
            child: Image.asset(
              'images/icon_guide.png',
              width: double.infinity,
              height: double.infinity,
              fit: BoxFit.fill,
            ),
          ),
          Offstage(
            offstage: _status != 2,
            child: ObjectUtil.isEmpty(_bannerItem)
                ? Container()
                : Swiper(
                    autoStart: false,
                    circular: false,
                    indicator: CircleSwiperIndicator(
                      radius: 3.5,
                      spacing: 5.0,
                      padding: EdgeInsets.only(bottom: 15.0),
                      itemActiveColor: Colors.white,
                      itemColor: Colors.white30,
                    ),
                    children: _bannerItem,
                  ),
          ),
          Offstage(
            offstage: _status != 1,
            child: Container(
              alignment: Alignment.bottomRight,
              margin: EdgeInsets.all(20.0),
              child: InkWell(
                onTap: () => _goMain(),
                child: Container(
                  padding: EdgeInsets.all(12.0),
                  decoration: BoxDecoration(
                      color: Color(0x66000000),
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                      border:
                          Border.all(width: 0.33, color: Color(0xffe5e5e5))),
                  child: Text(
                    '跳过 ${_seconds}s',
                    style: TextStyle(fontSize: 14.0, color: Colors.white),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  void _goMain() => Navigator.of(context).pushReplacementNamed(homePage);

  @override
  void dispose() {
    super.dispose();
    _timerUtil?.cancel();

    // 恢复状态栏和导航栏
    SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
  }
}
